package lancs.mobilemedia.core.ui.controller.commands;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Display;

import lancs.mobilemedia.core.ui.controller.AbstractController;
import lancs.mobilemedia.core.ui.controller.PhotoController;
import lancs.mobilemedia.core.ui.datamodel.AlbumData;
import lancs.mobilemedia.core.ui.datamodel.ImageData;
import lancs.mobilemedia.lib.exceptions.ImageNotFoundException;
import lancs.mobilemedia.lib.exceptions.NullAlbumDataReference;

public class SortableViewPhoto extends ViewPhoto {

	public void increasePhotoNumberOfViews(AbstractController controller, String selectedImageName) {
		super.increasePhotoNumberOfViews(controller, selectedImageName);

		try {
			ImageData image = (ImageData) controller.getModel().getImageInfo(selectedImageName);
			image.increaseNumberOfViews();
			((PhotoController) controller).updateImage(image);
			System.out.println("<* BaseController.handleCommand() *> Image = " + selectedImageName + "; # views = "
					+ image.getNumberOfViews());
		} catch (ImageNotFoundException e) {
			Alert alert = new Alert("Error", "The selected photo was not found in the mobile device", null,
					AlertType.ERROR);
			Display.getDisplay(controller.getMidlet()).setCurrent(alert,
					Display.getDisplay(controller.getMidlet()).getCurrent());
		} catch (NullAlbumDataReference e) {
			((PhotoController) controller).setAlbumData(new AlbumData());
			Alert alert = new Alert("Error", "The operation is not available. Try again later !", null, AlertType.ERROR);
			Display.getDisplay(controller.getMidlet()).setCurrent(alert,
					Display.getDisplay(controller.getMidlet()).getCurrent());
		}
	}

}
